package com.qq.servlet;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qq.bean.Dynamics;
import com.qq.service.DynamicsService;
import com.qq.service.impl.DynamicsServiceImpl;
//发布动态
@MultipartConfig
@WebServlet("/ReleaseDy.do")
public class ReleaseDyServlet extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DynamicsService dy = new DynamicsServiceImpl();
		String mysendtext = request.getParameter("mysendtext");
//		String dyImg = request.getParameter("dyImg");
		String me = request.getParameter("me");
		String nickName = request.getParameter("nickName");
		String myHeadimg = request.getParameter("myHeadimg");
//		Part part = request.getPart("dyImg");
//		System.out.println(part);
//		String fileName = UUID.randomUUID()+"_"+part.getSubmittedFileName();
//		// 2确定 存储路径(路径+存储的文件的文件名)(绝对路径)
//		String savePath = "c:/qq/headimgs/"+fileName;
//		// 3确定访问路径(img标签中 需要写的路径)
//		String visitPath = "/qq/headImgs/"+fileName;
//		// 4文件需要复制到 存储路径
//		part.write(savePath);
//		// 5执行添加业务，数据库里存访问路径
//		System.out.println(visitPath);
		Dynamics dynamics = new Dynamics(me,nickName,myHeadimg,mysendtext);
		if(dynamics.getDynamicsImage()==null) {
			dy.insertmydy(dynamics);
		}
//		dynamics.setDynamicsImage(visitPath);
//		if(selectdy == -1){
//			File file = new File(savePath);
//			if(file.exists()) {
//				file.delete();
//			}
//		}
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(dynamics);
		response.setContentType("application/json;charset=utf-8");
		mapper.writeValue(response.getWriter(), json);
	}
}
